import  pandas as pd
import re
import requests
import json

df = pd.read_csv('人工审核5月30.csv',sep='\001')


#calltext


local_url = "https://qa-beetle.amh-group.com/beetle/contraband/contraband_identify"
dev_url = "https://beetle.amh-group.com/beetle/contraband/contraband_identify"


def split_dialogue_into_listn( text):
    '''
    将对话分割成\n连接的形式
    @param item:
    @return:
    '''
    result = re.findall('[S|D]\：[^SD]*', text)
    return list(result)

def get_json(url,text):
    header = {"Content-Type": "application/json"}
    data = {"dialogue": [split_dialogue_into_listn(text)]}

    out_json = None
    try_count = 0
    while try_count < 3:
        req = requests.post(url,data=json.dumps(data), headers=header)
        if req.status_code == 200:
            out_json = req.json()
            break
        try_count += 1

    return out_json


def check_list_same(listA,listB):
    if not listA and not listB:
        return True
    listA = [ tuple(i) for i in listA ]
    listB = [ tuple(i) for i in listB]

    listA.sort(key=lambda j:j[0])
    listB.sort(key=lambda j:j[0])
    same = False
    if len(listA) == len(listB):
        for A,B in zip(listA,listB):
            if A != B:
                same = False
                break
            else:
                same = True

    return same




def apply_check_ok(text):
    old_json = get_json(dev_url,text)
    new_json = get_json(local_url,text)

    if "origin_dialogue" not in old_json["contraband_infos"][0]:
        print(old_json)
        print(new_json)
        return

    if check_list_same(old_json["contraband_infos"][0]["origin_dialogue"]["label"],new_json["contraband_infos"][0]["origin_dialogue"]["label"]):
        pass
    else:
        print("#############################")
        print(old_json)
        print(new_json)
        print("error!~~~")
        print("#############################")




df.dropna(subset=['calltext'],inplace=True)
df['calltext'].apply(apply_check_ok)

